package com.ryan.mdp.advice.service;

import java.util.concurrent.atomic.AtomicLong;

/**
 * 方法调用的性能指标
 * 
 */
public class Metrics {
    private MetricsKey key;

    // 调用次数
    private AtomicLong invokeCount = new AtomicLong(0);
    // 总耗时：单位，毫秒
    private AtomicLong totalTime = new AtomicLong(0);
    // 最后调用时间戳，单位，毫秒
    private long lastTimestamp;

    public Metrics() {
    }

    public Metrics(MetricsKey key) {
        this.key = key;
    }

    /**
     * 服务方法每次触发后执行此方法，记录指标
     * 
     * @param totalTime
     */
    public void add(long escapedTime) {
        invokeCount.addAndGet(1);
        totalTime.addAndGet(escapedTime);
        lastTimestamp = System.currentTimeMillis();
    }

    public MetricsKey getKey() {
        return key;
    }

    public void setKey(MetricsKey key) {
        this.key = key;
    }

    public AtomicLong getInvokeCount() {
        return invokeCount;
    }

    public void setInvokeCount(AtomicLong invokeCount) {
        this.invokeCount = invokeCount;
    }

    public AtomicLong getTotalTime() {
        return totalTime;
    }

    public void setTotalTime(AtomicLong totalTime) {
        this.totalTime = totalTime;
    }

    public long getLastTimestamp() {
        return lastTimestamp;
    }

    public void setLastTimestamp(long lastTimestamp) {
        this.lastTimestamp = lastTimestamp;
    }

    public String toString() {
        return "Metrics [invokeCount=" + invokeCount + ", totalTime=" + totalTime + ", lastTimestamp=" + lastTimestamp
                + "]";
    }
}
